import {
    Router,
    RouteRecordRaw,
    createWebHistory,
    createRouter
} from 'vue-router'
import store from '../store/index'
import Discover from "../views/Discover/Discover.vue"
import Cover from "../views/Cover/Cover.vue"
import Login from "../views/Login/Login.vue"
import My from "../views/My/My.vue"
import Playlist from "../views/Playlist/Playlist.vue"
import LikeSongList from "../views/LikeSongList/LikeSongList.vue"

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/discover'
    },
    {
        path: '/discover',
        name: 'Discover',
        component: Discover,
    },
    {
        path: '/cover',
        name: 'Cover',
        component: Cover,
    },
    {
        path: '/login',
        name: 'Login',
        component: Login
    },
    {
        path: '/my',
        name: 'My',
        component: My,
        meta: {
            requireLogin: true
        }
    },
    {
        path: '/playlist/:id',
        name: 'Playlist',
        component: Playlist
    },
    {
        path: '/likesonglist',
        name: 'LikeSongList',
        component: LikeSongList
    }
]

const router: Router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    if (store.state.user || to.name === 'Cover' || !to.meta.requireLogin) {
        return next()
    } else {
        return next({
            name: 'Cover'
        })
    }
})

export default router
